package com.gree.scmservice.service.impl;

import com.gree.common.entity.ReturnVo;
import com.gree.common.utils.StringUtils;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.MenuDao;
import com.gree.scmservice.entity.TreeSelect;
import com.gree.scmservice.entity.smbse.Smbse06N;
import com.gree.scmservice.entity.smbse.SysMenuMo;
import com.gree.scmservice.service.GscmMenuService;
import org.nutz.dao.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GscmMenuServiceImpl implements GscmMenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    TransactionDefinition transactionDefinition;

    @Override
    public List<SysMenuMo> selectMenuList(SysMenuMo sysMenuMo) {
        List<SysMenuMo> list = new ArrayList<>();
        String filter = "";
        if(StringUtils.isNotEmpty(sysMenuMo.getMenuName())){
            filter+=" and sb06n.tnam = '"+sysMenuMo.getMenuName()+"'";
        }
        if (sysMenuMo.getStyp()==1) {
            filter+=" and sb06n.styp in (1,2,3)";
        }else {
            filter+=" and sb06n.styp="+sysMenuMo.getStyp();
        }
        selectMenuListRecur(String.valueOf(sysMenuMo.getParentId()),filter,list);
        return list;
    }

    @Override
    public SysMenuMo selectMenuById(String menuId) {
        Record record = menuDao.fetchMenuByTeno("400",menuId);
        SysMenuMo sysMenuMo = new SysMenuMo();
        sysMenuMo.setMenuId(record.getLong("tono"));
        sysMenuMo.setMenuName(record.getString("tnam"));
        sysMenuMo.setParentId(record.getLong("fono"));
        sysMenuMo.setOrderNum(record.getString("tasc"));
        sysMenuMo.setPath(record.getString("turl"));
        sysMenuMo.setPano(record.getString("pano"));
        sysMenuMo.setIcon(record.getString("timg"));
        sysMenuMo.setDsca(record.getString("tdsc"));
        sysMenuMo.setStyp(record.getInt("styp"));
        return sysMenuMo;
    }

    @Override
    public List<Integer> selectMenuListByRoleKey(String roleKey) {
        List<Integer> result = new ArrayList<>();
        List<Record> list = menuDao.querySmbse08ByRoid(SecurityUtils.getUser().getUser().getComp(),roleKey);
        for(Record record : list){
            result.add(record.getInt("TENO"));
        }
        return result;
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<SysMenuMo> menus) {
        return menus.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertMenu(SysMenuMo menu) {
        Smbse06N smbse06N = menu.toSmbse06N();
        String menuid = smbse06N.getTeno();
        smbse06N = menuDao.insertSmbse06N("400",smbse06N);
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try{
            Long parentid = menu.getParentId();
            String orderNum = menu.getOrderNum();
            menuDao.insertSmbse07N(parentid,menuid,orderNum);
            if(smbse06N!=null){
                dataSourceTransactionManager.commit(transactionStatus);
                return 1;
            }else{
                dataSourceTransactionManager.rollback(transactionStatus);
                return 0;
            }
        }catch (Exception e) {
            dataSourceTransactionManager.rollback(transactionStatus);
            return 0;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateMenu(SysMenuMo menu) {
        int result = 0;
        Smbse06N smbse06N = menu.toSmbse06N();
        String menuid = smbse06N.getTeno();
        Long parentid = menu.getParentId();
        String orderNum = menu.getOrderNum();
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try{
            result+= menuDao.updateSmbse06N("400",smbse06N);
            result+= menuDao.updateSmbse07N(parentid,menuid,orderNum);
            dataSourceTransactionManager.commit(transactionStatus);
            return result;
        }catch (Exception e) {
            dataSourceTransactionManager.rollback(transactionStatus);
            return 0;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteMenuById(String menuId) {
        int result = 0;
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try{
            result+= menuDao.deleteSmbse06N("400",menuId);
            result+= menuDao.deleteSmbse07N(menuId);
            dataSourceTransactionManager.commit(transactionStatus);
            return result;
        }catch (Exception e) {
            dataSourceTransactionManager.rollback(transactionStatus);
            return 0;
        }
    }

    @Override
    public boolean checkMenuIDUnique(SysMenuMo menu) {
        Smbse06N smbse06N = menuDao.fetchSmbse06NByTeno("400",menu.getMenuId());
        return smbse06N == null?false:true;
    }

    @Override
    public boolean hasChildByMenuId(String menuId) {
        List<Record> list = menuDao.querySmbse07NByFono(menuId);
        return list.size() > 0 ? true : false;
    }

    @Override
    public boolean checkMenuExistRole(String menuId) {
        List<Record> list = menuDao.querySmbse08ByTeno(SecurityUtils.getUser().getUser().getComp(),menuId);
        return list.size() > 0 ? true : false;
    }

    @Override
    public void selectMenuListRecur(String teno,String filter,List dicList) {
        List<Record> list = menuDao.findMenuByFono(teno,filter);
        for (Record record : list) {
            List<SysMenuMo> childList = new ArrayList<SysMenuMo>();
            if (record != null) {
                SysMenuMo sysMenuMo = new SysMenuMo();
                sysMenuMo.setOrderNum(record.getString("TASC"));
                sysMenuMo.setPath(record.getString("TURL"));
                sysMenuMo.setMenuName(record.getString("TNAM"));
                sysMenuMo.setDsca(record.getString("TDSC"));
                sysMenuMo.setIcon(record.getString("TIMG"));
                sysMenuMo.setStyp(record.getInt("STYP"));
                sysMenuMo.setMenuId(record.getLong("TONO"));
                sysMenuMo.setParentId(record.getLong("FONO"));
                sysMenuMo.setPano(record.getString("PANO"));
                selectMenuListRecur(record.getString("TENO"),"",childList);
                sysMenuMo.setChildren(childList);
                dicList.add(sysMenuMo);
            }
        }
    }


}
